package mortal.game.control
{
    import Message.Game.*;
    import com.gengine.global.*;
    import flash.events.*;
    import flash.geom.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.rules.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildShopController extends Controller
    {
        private var _shopModule:GuildShopModule;

        public function GuildShopController()
        {
            Dispatcher.addEventListener(EventName.GuildOpenShop, this.onGuildOpenShopHandler);
            Dispatcher.addEventListener(EventName.GuildGetShopItems, this.onGuildGetShopItemsHandler);
            Dispatcher.addEventListener(EventName.GuildGetShopItemsSuccess, this.onGuildGetShopItemsSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildBuyShopItem, this.onGuildBuyShopItemHandler);
            
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        override protected function initView() : IView
        {
            if (this._shopModule == null)
            {
                this._shopModule = new GuildShopModule();
                this._shopModule.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            return this._shopModule;
        }

        private function onAddedToStageHandler(event:Event) : void
        {
            NpcEffectRule.registDialogEffectWindow(this.view);
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetShopItems));
            this._shopModule.setSelectedBuyBtn();
            
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.GuildShopUpdate, this.onGuildShopUpdateHandler);
            
        }

        private function onGuildShopUpdateHandler(param1:SGuildShopUpdate = null) : void
        {
            var _loc_2:Array = null;
            if (this._shopModule && param1)
            {
                _loc_2 = param1.shopItems;
                if (param1.reset == true)
                {
                    this._shopModule.setDefaultPage(_loc_2);
                }
                else
                {
                    this._shopModule.replaceShopItem(_loc_2[0]);
                }
            }
            
        }

        private function onGuildGetShopItemsHandler(event:DataEvent) : void
        {
            GameProxy.guildProxy.getGuildShopItems();
            
        }

        private function onGuildGetShopItemsSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            _loc_2 = cache.guild.getSortedGuildShopItems(_loc_2);
            this._shopModule.setDefaultPage(_loc_2);
            
        }

        private function onGuildBuyShopItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["itemCode"] as int;
            var _loc_3:* = event.data["amount"] as int;
            GameProxy.guildProxy.buyGuildShopItem(_loc_2, _loc_3);
            
        }

        private function onGuildOpenShopHandler(event:DataEvent) : void
        {
            var _loc_2:* = (Global.stage.stageHeight - GameController.pack.view.height) / 2;
            this.view.show(380, _loc_2);
            GameController.pack.view.show(710, _loc_2);
            
        }

        private function onChangeSceneHandler(event:DataEvent) : void
        {
            if (this._shopModule && this._shopModule.isHide == false)
            {
                this._shopModule.hide();
            }
            
        }

        private function onGirdWalkEnd(event:PlayerEvent) : void
        {
            var _loc_3:NPCInfo = null;
            var _loc_4:Point = null;
            var _loc_5:Number = NaN;
            var _loc_2:* = RolePlayer.instance.currentPoint;
            if (this._shopModule && this._shopModule.isHide == false)
            {
                _loc_3 = cache.dialog.npcInfo;
                if (_loc_3)
                {
                    _loc_4 = GameMapUtil.getPixelPoint(_loc_3.snpc.point.x, _loc_3.snpc.point.y);
                    _loc_5 = Math.sqrt(Math.pow(_loc_4.x - _loc_2.x, 2) + Math.pow(_loc_4.y - _loc_2.y, 2));
                    if (_loc_5 > 120)
                    {
                        this.onChangeSceneHandler(null);
                    }
                }
            }
            
        }

    }
}
